using AppToolKit.Core.Logging;
using Common.Logging;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace AppToolKit.Core.AttributeDiscovery
{
	public class AssemblyDetector
	{
		private static ILog logger_ = LogManager.GetLogger(DefaultLogger.LOGGER_NAME);
		public List<IFeatureProcessor> Processes
		{
			get;
			set;
		}
		private Assembly[] GetAssemblies()
		{
			return AppDomain.CurrentDomain.GetAssemblies();
		}
		public void Init()
		{
			Assembly[] assemblies = this.GetAssemblies();
			FeatureProcessContext featureProcessContext = new FeatureProcessContext();
			Assembly[] array = assemblies;
			for (int i = 0; i < array.Length; i++)
			{
				Assembly assembly = array[i];
				if (AssemblyDetector.logger_.IsDebugEnabled)
				{
					AssemblyDetector.logger_.Debug(string.Format("==== load  assembly({0})======", assembly.FullName));
				}
			}
			if (AssemblyDetector.logger_.IsDebugEnabled)
			{
				AssemblyDetector.logger_.Debug("开始detector assembly");
			}
			array = assemblies;
			for (int i = 0; i < array.Length; i++)
			{
				Assembly assembly2 = array[i];
				if (AssemblyDetector.logger_.IsDebugEnabled)
				{
					AssemblyDetector.logger_.Debug(string.Format("===========正在检查程序集{0}==========", assembly2));
				}
				foreach (IFeatureProcessor current in this.Processes)
				{
					if (current.IsSupport(assembly2))
					{
						AssemblyDetector.logger_.Debug(string.Format("===========support {0}==========", assembly2));
						AssemblyDetector.logger_.Debug(string.Format("process assembly{0}===========", assembly2));
						current.ProcessAssembly(assembly2);
						AssemblyDetector.logger_.Debug(string.Format("process assembly{0} end====", assembly2));
						Type[] types = assembly2.GetTypes();
						for (int j = 0; j < types.Length; j++)
						{
							Type type = types[j];
							current.Process(type, featureProcessContext);
						}
					}
				}
				if (AssemblyDetector.logger_.IsDebugEnabled)
				{
					AssemblyDetector.logger_.Debug(string.Format("===========检查程序集{0}完毕==========", assembly2));
				}
			}
			this.AfterInit(featureProcessContext);
		}
		private void AfterInit(FeatureProcessContext ctx)
		{
			foreach (IInitUnit current in ctx.InitialUnit)
			{
				current.Process();
			}
		}
	}
}
